/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package operaciones;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.component.EditableValueHolder;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author chokurey
 */
public class FileUploadControl implements Serializable {
   
     UIComponent componente;
     Object valor=null;
     private UploadedFile file;
     private String destination="C:\\Users\\chokurey\\Documents\\NetBeansProjects\\trueque\\web\\archivos\\",urllogo;

    public String getUrllogo() {
        return urllogo;
    }

    public void setUrllogo(String urllogo) {
        this.urllogo = urllogo;
    }
    
     
    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }
     
     
     
     
      public void TransferFile(String fileName, InputStream in)
    {
      try{
         OutputStream out = new FileOutputStream(new File(destination+fileName));
         int reader = 0;
         byte bytes[] = new byte[(int)getFile().getSize()];
         while((reader=in.read(bytes)) != -1)
         {
             out.write(bytes, 0, reader);
         }
         in.close();
         out.flush();
         out.close();
      }catch(IOException e)
       {
         System.out.print(e.getMessage());
       }
    
    }
    
    public void upload()
    {
       String extValidate;
       
       //componente = e.getComponent().findComponent("fileuploadlogo");
     //  valor=((EditableValueHolder)componente).getValue();
       
       if(getFile() != null)
       {
         String ext= getFile().getFileName();
         if(ext !=null)
         { extValidate=ext.substring(ext.indexOf(".")+1); }
         else
         { extValidate=null; }
         
         if(extValidate.equals("jpeg") || extValidate.equals("jpg"))
         { 
           try{
                TransferFile(getFile().getFileName(), getFile().getInputstream());
              } catch(IOException ex){
                Logger.getLogger(FileUploadControl.class.getName()).log(Level.SEVERE, null, ex);
                FacesContext context =FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage("Error"," Al subir el archivo"));
              } 
                FacesContext context =FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage("Correcto",getFile().getFileName()+ " tipo de archivo "+getFile().getContentType()+" Tamaño: "+getFile().getSize()));
               // jpapernat=new PersonanaturalJpaController();
               // pernat=jpapernat.findPersonanatural(3);
                
            
                
               //pernat.setFoto("C:\\Users\\chokurey\\Documents\\NetBeansProjects\\portal web\\web\\upload\\"+getFile().getFileName());
               
                setUrllogo(getFile().getFileName());
                
                
           
         }else{
              FacesContext context =FacesContext.getCurrentInstance();
              context.addMessage(null, new FacesMessage("Error de Extension"," la extension debe ser jpg"));
           }
       
       }else{
               FacesContext context =FacesContext.getCurrentInstance();
              context.addMessage(null, new FacesMessage("Error "," Seleccione un archivo"));
       }
    
    }
    
}
